package br.edu.ifrn.tads.psw.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.edu.ifrn.tads.psw.dao.Dao;
import br.edu.ifrn.tads.psw.model.Usuario;

@SuppressWarnings("serial")
public class LoginController extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		Dao dao = new Dao();
		Usuario usuario = dao.getUsuario(login);
		if (usuario != null && senha.equals("1234")) {
			request.setAttribute("usuario", usuario);
			RequestDispatcher view = request.getRequestDispatcher("/inicial.jsp");
			try {
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			request.setAttribute("mensagem", "Falha na autenticação");
			RequestDispatcher view = request.getRequestDispatcher("/login.jsp");
			try {
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}
